package cn.com.dhc.danlu.activity.delivery.order.viewholder;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.delivery.order.bean.OrderStatusBean;
import cn.com.dhc.danlu.activity.delivery.order.decorate.Visitable;
import cn.com.dhc.danlu.activity.delivery.order.delegate.IHolderDataCallback;
import cn.com.dhc.danlu.dlware.global.DanluApplication;
import cn.com.dhc.danlu.util.CommonConst;

import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.BTN_NO;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.DEAL_COMPLETE;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.HAS_ORDER_REFUND;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.ORDER_CANCLE;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.ORDER_DEAL_CANCLING;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.ORDER_DEAL_CANCLING_CONSTANT;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.WAIT_PAY;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.WAIT_RECEIVE;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.WAIT_SEND;
import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst._ORDER_PAYING;


/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-02-14 15:16
 * <p>
 * <p>
 * 订单列表--交易号、订单号等视图
 * </p>
 */
public class OrderStatusViewHolder extends BaseViewHolder<OrderStatusBean> {
    private TextView tvPayNo; //交易号
    private TextView tvOrderNo; // 订单号
    private TextView tvPayStatus; // 交易状态
    private TextView tvOrderStatus; // 订单状态
    private ImageView ivSplitFlag; // 订单拆分标志

    private IHolderDataCallback mListener;

    public OrderStatusViewHolder(View itemView, IHolderDataCallback callback) {
        super(itemView);
        this.mListener = callback;

        tvPayNo = (TextView) itemView.findViewById(R.id.tv_pay_no);
        tvPayStatus = (TextView) itemView.findViewById(R.id.tv_pay_status);
        tvOrderNo = (TextView) itemView.findViewById(R.id.tv_order_no);
        tvOrderStatus = (TextView) itemView.findViewById(R.id.tv_order_status);
        ivSplitFlag = (ImageView) itemView.findViewById(R.id.iv_split_flag);

    }

    /**
     * 设置订单状态 及 拆单标志显/隐
     *
     * @param data
     *         订单状态数据
     */
    private void setOrderStatus(Visitable data) {
        String orderStatus = ((OrderStatusBean) data).getOrderStatus();
        String buttomList = ((OrderStatusBean) data).getButtomList();
        String[] split = buttomList.split("");
        String orderSplit = split[8];
        ivSplitFlag.setVisibility(orderSplit.equals("1") ? View.VISIBLE : View.GONE);
        switch (orderStatus) {
            case WAIT_PAY: // 待付款
                tvOrderStatus.setText(DanluApplication.get().getString(R.string.myself_deal_wait_pay));
                tvOrderStatus.setTextColor(DanluApplication.get().getResources().getColor(R.color.color_b12525));
                break;
            case WAIT_SEND: // 待发货
                tvOrderStatus.setText(DanluApplication.get().getString(R.string.myself_deal_wait_send));
                tvOrderStatus.setTextColor(DanluApplication.get().getResources().getColor(R.color.color_b12525));
                break;
            case WAIT_RECEIVE: // 待收货
                tvOrderStatus.setText(DanluApplication.get().getString(R.string.myself_deal_wait_receive));
                tvOrderStatus.setTextColor(DanluApplication.get().getResources().getColor(R.color.color_b12525));
                break;
            case DEAL_COMPLETE: // 交易完成
                tvOrderStatus.setText(DanluApplication.get().getString(R.string.myself_deal_complete));
                tvOrderStatus.setTextColor(DanluApplication.get().getResources().getColor(R.color.color_028b05));
                break;
            case ORDER_CANCLE: // 取消订单
                tvOrderStatus.setText(DanluApplication.get().getString(R.string.myself_order_cannel));
                tvOrderStatus.setTextColor(DanluApplication.get().getResources().getColor(R.color.translucent_light_gray_black));
                break;
            case HAS_ORDER_REFUND:
                tvOrderStatus.setText(DanluApplication.get().getString(R.string.myself_order_cannel));
                tvOrderStatus.setTextColor(DanluApplication.get().getResources().getColor(R.color.translucent_light_gray_black));
                break;
            case _ORDER_PAYING:  //支付中
                tvOrderStatus.setText(DanluApplication.get().getString(R.string.myself_deal_wait_paying));
                tvOrderStatus.setTextColor(DanluApplication.get().getResources().getColor(R.color.color_b12525));
                break;
            default:
                tvOrderStatus.setText("");
                break;
        }
    }

    /**
     * 设置交易按钮
     *
     * @param data
     *         订单状态数据
     */
    private void setPayStatus(Visitable data) {
        String buttomList = ((OrderStatusBean) data).getButtomList();
        String[] split = buttomList.split("");
        //        boolean isShowCancelDeal = !split[4].equals(BTN_NO) && !split[4].equals("4");
        if (split[4].equals(BTN_NO)) {
            tvPayStatus.setVisibility(View.GONE);
        } else {
            tvPayStatus.setVisibility(View.VISIBLE);
            if (split[4].equals(ORDER_DEAL_CANCLING)) {
                tvPayStatus.setVisibility (View.VISIBLE);
                tvPayStatus.setText(ORDER_DEAL_CANCLING_CONSTANT);
            } else {
                tvPayStatus.setVisibility (View.GONE);
            }
        }
    }

    /**
     * 是否存在通过易极付支付的订单
     */
    private boolean isExistFinishPayByYjf(Visitable data) {
        boolean isExist = false;
        if (data != null) {
            String payWay = ((OrderStatusBean) data).getPayWay();
            if (!TextUtils.isEmpty(payWay) && CommonConst.PaymentConst.PAY_WAY_YJF.equals(payWay)) {
                isExist = true;
            }
        }
        return isExist;
    }
    @Override
    protected void bindViewData(OrderStatusBean data) {
        tvPayNo.setText(data.getPaymentNo());
        tvOrderNo.setText(data.getOrderNo());
        setPayStatus(data);
        setOrderStatus(data);
    }
}
